<?php
namespace app\index\controller;

use app\common\service\MqttService;
use think\Controller;

class Demo 
{
    public function sendMqtt()
    {
        
        $topic = 'testtopic/1';   // 目标主题
        $message = json_encode(['data' => 'Hello MQTT from ThinkPHP']);

        $mqttService = new MqttService();
        if ($mqttService->publish($topic, $message)) {
            return json(['code' => 200, 'msg' => '消息发送成功']);
        } else {
            return json(['code' => 500, 'msg' => '消息发送失败']);
        }
    }
}